<?php

// +----------------------------------------------------------------------
// | Admin Plugin for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2024 ThinkAdmin [ thinkadmin.top ]
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/think-plugs-admin
// | github 代码仓库：https://github.com/zoujingli/think-plugs-admin
// +----------------------------------------------------------------------

declare(strict_types=1);

namespace app\paths\controller;

use app\common\model\paths\ClassModel;
use app\common\model\paths\ClassTaskModel;
use app\common\model\paths\EmployInfoModel;
use app\common\model\paths\EmployLogModel;
use app\common\model\paths\EmployModel;
use app\common\model\paths\TaskChapterModel;
use app\common\model\paths\TaskModel;
use app\common\model\paths\UserClassModel;
use app\common\model\paths\UserStudyGoldModel;
use app\common\model\index\UserModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\admin\helper\QueryHelper;
use think\facade\Validate;
use think\file\UploadedFile;

/**
 * 学生成绩查询
 * @class Base
 * @package app\admin\controller
 */
class UserGoldShow extends Controller
{

    public $check_user = false;
    /**
     * 学生成绩查询
     * @auth true
     * @menu true
     * @login true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index ()
    {


        UserStudyGoldModel::mQuery()->layTable(function () {

            $this->class_arr =  ClassModel::mk()->column('name', 'id');

            $this->task_arr =  TaskModel::mk()->column('title', 'id');

        }, function (QueryHelper $query) {
            $query->alias('usg');
            $query->join('index_user iu', 'iu.id = usg.index_user_id', 'left');
            $query->field('usg.index_user_id, usg.class_id, usg.task_id,sum(usg.total_gold) as total_gold,iu.true_name as user_name');
            $query->group('index_user_id,class_id,task_id');
            $query->like('iu.true_name#user_name')->equal('class_id,task_id');
        });

    }

    protected function _index_page_filter(&$data)
    {

        array_map(function ($item) use (&$class_ids, &$task_ids ) {
            $class_ids[]        = $item['class_id'];
            $task_ids[]         = $item['task_id'];
        }, $data);


        $task_name  = TaskModel::mk()->whereIn('id', $task_ids)->column('title', 'id');
        $class_name = ClassModel::mk()->whereIn('id', $class_ids)->column('name', 'id');


        foreach ($data as &$item) {

            $item['task_name'] = $task_name[$item['task_id']] ?? '';
            $item['class_name'] = $class_name[$item['class_id']] ?? '';
        }

    }


    public function info ()
    {



        EmployLogModel::mQuery()->layTable(function () {
            $this->index_user_id = $this->request->get('index_user_id', 0);
            $this->task_id = $this->request->get('task_id', 0);

        }, function (QueryHelper $query) {
            
            $query->equal('index_user_id,task_id');
            $query->where('gold', ">", 0);
        });
    }

    protected function  _info_page_filter (&$data)
    {

        $employ_ids = $task_ids = [];
        array_map(function ($item) use (&$employ_ids , &$task_ids) {

            if ($item['type'] == 1) {

                $employ_ids[] = $item['levels_id'];
            } else {
                $task_ids[]   = $item['levels_id'];
            }

        }, $data);

        // 找到 实战  和 课程 信息
        $employ_title = EmployInfoModel::mk()->whereIn('id', $employ_ids)->column('title', 'id');
        $task_title   = TaskChapterModel::mk()->whereIn('id', $task_ids)->column('title', 'id');

        $type_name = EmployLogModel::getTypeName();

        foreach ($data as &$item) {

            $item['type_name'] = $type_name[$item['type']];

            $item['title']     = $item['type'] == 1 ? $employ_title[$item['levels_id']] : $task_title[$item['levels_id']];
        }
    }
}